const sqlhelper = require('./sqlhelper');
const connection = sqlhelper.sqlConnection;

module.exports={
    // 从服务器获取所有的文章
    getAllPosts(callback){
        let sql = `SELECT posts.id,title,posts.\`status\`,created,nickname,\`name\` 
        FROM posts 
        JOIN users ON posts.user_id=users.id
        JOIN categories ON posts.category_id=categories.id`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 通过页数获取页面
    getPostByPage(pageIndex,pageSize,callback){
        let sql = `SELECT posts.id,title,posts.\`status\`,created,nickname,\`name\` 
        FROM posts 
        JOIN users ON posts.user_id=users.id
        JOIN categories ON posts.category_id=categories.id    
        LIMIT ${(pageIndex-1)*pageSize},${pageSize}`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 获取总条数的代码
    getPostCount(condition,callback){
        let sql = `SELECT COUNT(*) as total FROM posts `+condition;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
        
    },
    // 根据分类的id进行数据的筛选
    getPostsByFilter(sql,callback){    
        connection.query(sql,(err,result)=>{
          callback(err,result);
        })
      },
    //   新增一个新的文章数据
    addNewPost(data,callback){
        let sql = `insert into posts set ?`;
        connection.query(sql,data,(err,result)=>{
            callback(err,result);
          })
    }
}